package com.bnpparibas.tools.ant.logic.select;
/***
 * Task definition for the select task.  The select task iterates
 * over a list of param.
 *
 * <pre>
 *
 * Usage:
 *
 *   Task declaration in the project:
 *   <code>
 *     &lt;taskdef name="select" classname="net.sf.antcontrib.logic.ForEach" /&gt;
 *   </code>
 *
 *   Call Syntax:
 *   <code>
 *     &lt;foreach list="values" target="targ" param="name"
 *                 [delimiter="delim"] /&gt;
 *   </code>
 *
 *   Attributes:
 *         list      --> The list of values to process, with the delimiter character,
 *                       indicated by the "delim" attribute, separating each value
 *         target    --> The target to call for each token, passing the token as the
 *                       parameter with the name indicated by the "param" attribute
 *         param     --> The name of the parameter to pass the tokens in as to the
 *                       target
 *         delimiter --> The delimiter string that separates the values in the "list"
 *                       parameter.  The default is ","
 *
 * </pre>
 * @author <a href="mailto:mattinger@mindless.com">Matthew Inger</a>
 */
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Property;
import org.apache.tools.ant.taskdefs.Sequential;

/**
 * @author V_Thoule
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class SelectTask extends Task  {
    private Sequential paramTasks = null;
	private String property;
	private String message;
    private Vector params;
    private boolean Exclusive = true;
    private String listDelimiter = ",";
    private String propertyDelimiter = ":";
    
    private int defaultParam;

	public SelectTask() {
        this.params = new Vector();
	}

	public void setProperty(String property) {
		this.property = property;
	}

    public void setPropertyDelimiter(String propertyDelimiter)
    {
        this.propertyDelimiter = propertyDelimiter;
    }
	
    public void setListDelimiter(String listDelimiter)
    {
        this.listDelimiter = listDelimiter;
    }

	/**
	 * Add New Property for Selection
	 */
	private void setNewProperty(String property) {
		StringTokenizer st = new StringTokenizer(property, propertyDelimiter);
		String tok1 = new String();
		String tok2 = new String();
            
		if (st.hasMoreTokens()) {
			
			tok1 = st.nextToken();
			Property prop = new Property();
			prop.setName(tok1);

			if (st.hasMoreTokens()) {
				tok2 = st.nextToken();
				prop.setValue(tok2);
			} else {
				prop.setValue(tok1);
			}		
			params.addElement(prop);
		}
	}

            
	/**
	 * Fill Parameters depending on Properties List with the following syntax :
	 * 	Name:Value,Name:Value
	 */
	public void setPropertyList(String propertiesList) {
		StringTokenizer st = new StringTokenizer(propertiesList, this.listDelimiter);
		while (st.hasMoreTokens()) {
			String tok = st.nextToken();
			setNewProperty(tok);
		}
	}

	public void setDefaultParam(String defaultParam) {
		this.defaultParam = Integer.parseInt(defaultParam);
	}
	
	public void setExclusive(String exclusive) {
		if (exclusive.equalsIgnoreCase("true") ) {
			this.Exclusive = true;
		} else if (exclusive.equalsIgnoreCase("false")) {
			this.Exclusive = false;
		} else {
			throw new BuildException("Invalid value for 'exclusive'. true assumed if ommited.");
		}
	}
	
	public void setMessage(String message) {
		this.message = message;
	}

    public void addParam(Property p) {
        params.addElement(p);
    }    

	public void execute() throws BuildException {
		ChoiceDef 	cd = new ChoiceDef();
		if (!(this.defaultParam >= 0 && this.defaultParam < params.size())) {
	    	this.defaultParam = 0;
        }
        Enumeration enumOnParams = params.elements();
        while (enumOnParams.hasMoreElements()) {
            Property param = (Property) enumOnParams.nextElement();
            cd.AddChoice(param.getName(),param.getValue());
        }
        cd.getChoice(this.defaultParam).setSelected(true);
		cd.IsExclusif(this.Exclusive);
		
		SelectFrame f = new SelectFrame(cd);
		if (f.isCancelled()) {
			throw new BuildException("Cancelled by user.");
		} else {
			javax.swing.JRadioButton[] JRadioButtons = f.getJRadioButtons();
			int i;
			int j=0;
			StringBuffer sbProperty = new StringBuffer();
			for (i = 0; i < JRadioButtons.length; i++) {
				if (JRadioButtons[i].isSelected()) {
					if (j>0) sbProperty.append(this.listDelimiter);
					System.out.println("Item " + i + " is selected ");
					sbProperty.append(cd.getChoice(i).Value);
					j++;
				}
			}
			System.out.println("Setting " + property  + " to " + sbProperty.toString());
			getProject().setProperty(property, sbProperty.toString());
		}
	}    
}
